/**
 * Copyright 2010 Butin Kirill <kiryaka@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.flexupload.status
{
	import org.flexupload.Flexupload;
	import org.flexupload.event.FileStatusEvent;

	public class FileStatus extends StatusBase
	{
		private static const STATUS_STARTING:int = 1; // file sent startUpload responce but havn't responce yet 
		private static const STATUS_PROCESSING:int = 2;
		private static const STATUS_FINISHED:int = 3;
		
		private var status:int = STATUS_STARTING;
		
		private var name:String;
		private var tmpFileName:String;
		private var size:int;
		
		private var threads:Array = new Array();
		public function FileStatus(name:String, size:int)
		{
			this.name = name;
			this.size = size;
		}
		override public function updateBytesTransfered(bytesTransfered:int):void{
			super.updateBytesTransfered(bytesTransfered);
			dispatchEvent(FileStatusEvent.UPDATE);
			// TODO refactoring
			Flexupload.getInstance().getUploadStatus().updateBytesTransfered(bytesTransfered);
		}
		private function dispatchEvent(type:String):void{
			var event:FileStatusEvent = new FileStatusEvent(type);
			event.setFileStatus(this);
			Flexupload.getInstance().dispatchEvent(event);
		}
		
		public function getSize():int{
			return size;
		}
		
		public function getFileName():String{
			return name;
		}
		
		public function getThreadCount():int{
			return threads.length;
		}		
		public function getThreadByIndex(index:int):FileThreadStatus{
			return FileThreadStatus(threads[index]);
		}
		public function addThread(fileThreadStatus:FileThreadStatus):void{
			this.threads.push(fileThreadStatus);
		}
		public function isFinished():Boolean{
			return status == STATUS_FINISHED;
		}
		public function setFinished():void{
			status = STATUS_FINISHED;
			dispatchEvent(FileStatusEvent.FINISH);
		}
		public function isProcessing():Boolean{
			return status == STATUS_PROCESSING;
		}
		public function setProcessing():void{
			status = STATUS_PROCESSING;
		}
		public function isStarting():Boolean{
			return status == STATUS_STARTING;
		}
		public function setStarting():void{
			status = STATUS_STARTING;
		}		
		public function getResidualBytes():int{
			return size - bytesTransfered;
		}
		public function setTmpFileName(tmpFileName:String):void{
			this.tmpFileName = tmpFileName;
		}
		public function getTmpFileName():String{
			return this.tmpFileName
		}		
	}
}